package edu.gdit.health.controller;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import edu.gdit.health.base.result.Results;
import edu.gdit.health.model.SysDict;
import edu.gdit.health.model.SysReport;
import edu.gdit.health.service.Impl.SysReportServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@Controller
//定义控制器的访问形式\report
@RequestMapping("/report")
public class ReportController {
    private static final Log log = LogFactory.getLog(ApiController.class);

    @Autowired
    SysReportServiceImpl sysReportServiceImpl;



    //以/report/add的Get形式访问，则进入/templates/report/add.html页面
    @RequestMapping("/add")
    public String  add(Model model){
        Map<String, List<SysDict>> dict=sysReportServiceImpl.getDictSet();
        model.addAttribute("dict",dict);
        return  "/report/add";
    }

    //以/report/add的Post形式访问，则进入下面的方法，回到/report/add.html页面
/*    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ModelAndView addPost(ModelAndView mv, SysReport sysReport) {
//        System.out.println("username:" + username);
//        System.out.println("password:" + pwd);
       System.out.println("remark:" + sysReport.getRemark());
       System.out.println("temperature:"+sysReport.getTemperature());
        //将username和pwd添加到mv对象
        mv.addObject("remark",sysReport.getRemark());
        mv.addObject("temperature",sysReport.getTemperature());
        //调用
        sysReportServiceImpl.addSysReport(sysReport);
        //设计转向的视图
        mv.setViewName("/addReport");
        return mv;
    }*/
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Results save(ModelAndView mv, @RequestBody SysReport sysReport){
        //调用业务层对象sysReportServiceImpl的addReport方法，增加一条记录
        return sysReportServiceImpl.addSysReport(sysReport);

    }

    @RequestMapping(value = "/list")
    @ResponseBody
//@RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的)；GET方式无请求体，所以使用@RequestBody接收数据时，前端不能使用GET方式提交数据，而是用POST方式进行提交。
    public Results list(int page,int limit){
        log.info("19软件4班用户打开了新页面");
        //spring mvc框架会自动获取参数赋值
        int offset=(page-1)*limit;
        //调用service层的对象sysReportServiceImpl的addSysReport方法，添加一条数据
        return sysReportServiceImpl.getReports(offset,limit);
    }


}

